//
//  CommentCell.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/16.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "CommentCell.h"

@interface CommentCell ()

@property(nonatomic, strong)UILabel *contentLabel;
@property(nonatomic, strong)NSMutableAttributedString *attributedString;

@end

@implementation CommentCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * identifier = @"CommentCell";
    CommentCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = [UIColor xm_grayColor];
        
        
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.font = [UIFont xm_font:15.0];
        self.contentLabel.textColor = [UIColor xm_jetblackColor];
        [self.contentView addSubview:self.contentLabel];
    }
    return self;
}
-(void)setNickName:(NSString *)nickName{
    _nickName = nickName;
}
-(void)setComment:(AticleComment *)comment{
    _comment = comment;
    if (_comment.isMore) {
        self.contentLabel.textColor = [UIColor xm_MainColor];
        self.contentLabel.text = [NSString stringWithFormat:@"查看全部%ld条回复",(long)self.commentCount -3];
    }else{
        self.contentLabel.textColor = [UIColor xm_jetblackColor];
        self.contentLabel.attributedText = [self getAttr:_comment];
    }
}

- (NSMutableAttributedString*)getAttr:(AticleComment *)model {
    NSString * string;
    if ([model.Content containsString:@"||"]) {
        
        NSRange range = [model.Content rangeOfString:@"||"];
        
        NSString * nameText = [model.Content substringWithRange:NSMakeRange(range.location + range.length, _comment.Content.length-(range.location+range.length))];
        
        string = [NSString stringWithFormat:@"%@回复%@:%@",model.NikeName,nameText,[model.Content substringWithRange:NSMakeRange(0,range.location)]];
        NSRange repleyRange = [string rangeOfString:@"回复"];
        NSMutableAttributedString * resultAttr = [[NSMutableAttributedString alloc] initWithString:string];
        [resultAttr addAttribute:NSForegroundColorAttributeName
                           value:[UIColor xm_MainColor]
                           range:[string rangeOfString:model.NikeName]];
        [resultAttr addAttribute:NSForegroundColorAttributeName
                           value:[UIColor xm_MainColor]
                           range:NSMakeRange(repleyRange.location + repleyRange.length, nameText.length)];
        

        return resultAttr;
    }else{
        string = [NSString stringWithFormat:@"%@:%@",model.NikeName,model.Content];
        NSMutableAttributedString * resultAttr = [[NSMutableAttributedString alloc] initWithString:string];
        [resultAttr addAttribute:NSForegroundColorAttributeName
                           value:[UIColor xm_MainColor]
                           range:[string rangeOfString:model.NikeName]];
        return resultAttr;
    }
    
    
}



-(NSMutableAttributedString *)attributedString{
    if (!_attributedString) {
        _attributedString = [[NSMutableAttributedString alloc] init];
    }
    return _attributedString;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(65);
        make.right.mas_equalTo(self).offset(-15);
        make.top.bottom.mas_equalTo(self);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self.contentView).offset(15);
        make.right.mas_equalTo(self.contentView).offset(-15);
    }];
}

@end
